package cn.momoshenchi.service.impl;

import cn.momoshenchi.pojo.Book;
import cn.momoshenchi.pojo.Tips;
import cn.momoshenchi.pojo.UserTips;
import cn.momoshenchi.service.UserTipsService;
import cn.momoshenchi.util.AssertUtil;
import cn.momoshenchi.util.ParamsException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.momoshenchi.service.TipsService;
import cn.momoshenchi.mapper.TipsMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

/**
 *
 */
@Service
public class TipsServiceImpl extends ServiceImpl<TipsMapper, Tips>
implements TipsService{

    @Autowired
    private UserTipsService userTipsService;

    @Autowired
    private  TipsMapper tipsMapper;

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Override
    @Transactional
    public Tips getRandomOne()
    {

        Tips tips = tipsMapper.getRandomOne();
        AssertUtil.isTrue(tips==null,"系统错误");
        return  tips;

    }

    @Override
    @Transactional(rollbackFor = ParamsException.class)
    public void insertWithEs(Tips tips) throws IOException
    {
        AssertUtil.isTrue(!this.save(tips),"添加失败");
//        IndexRequest indexRequest = new IndexRequest("tips");
//        indexRequest.id(tips.getId().toString());
//        ObjectMapper objectMapper = new ObjectMapper();
//        String s = objectMapper.writeValueAsString(tips);
//        indexRequest.source(s, XContentType.JSON);
//        IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT.toBuilder().build());
    }

    @Override
    @Transactional(rollbackFor = ParamsException.class)
    public void insertListWithEs(List<Tips> tipsList) throws IOException
    {
        AssertUtil.isTrue(!this.saveBatch(tipsList),"添加失败");
//        BulkRequest bulkRequest = new BulkRequest();
//        for (Tips tips : tipsList)
//        {
//            IndexRequest indexRequest = new IndexRequest("tips");
//            indexRequest.id(tips.getId().toString());
//            ObjectMapper objectMapper = new ObjectMapper();
//            String s = objectMapper.writeValueAsString(tips);
//            indexRequest.source(s, XContentType.JSON);
//            bulkRequest.add(indexRequest);
//        }
//        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT.toBuilder().build());
//        boolean failures = bulk.hasFailures();
    }

    @Transactional
    public Tips looked(Long userId)
    {
        //TODO: 这边要返回用户没有的一个
        List<UserTips> userTipsList = userTipsService
                .list(new QueryWrapper<UserTips>().eq("user_id", userId));

        Tips tips = tipsMapper.getRandomOneWithoutUser(userTipsList);

        // 如果用户读完了所有tips,那么tips为空.
        // 此时删除用户的记录.重新获取tips
        if(tips==null)
        {
            userTipsService.remove(new QueryWrapper<UserTips>().eq("user_id",userId));
            tips = tipsMapper.getRandomOne();
        }

        //用户获取后,在新表中添加记录
        UserTips tips1 = new UserTips();
        tips1.setUserId(userId);
        tips1.setTipsId(tips.getId());
        userTipsService.save(tips1);
        return  tips;

    }
}




